package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.User;
import com.zr.service.FunctionService;
import com.zr.service.UserService;
import com.zr.serviceimpl.UserServiceImpl;

@Controller
@Scope(value="prototype")
public class UserAction extends  ActionSupport implements SessionAware,ServletResponseAware{
	@Resource
	private UserService userService;
	@Resource
	private FunctionService functionService;
	
	
	private Map<String, Object> session; //获取session
	private HttpServletResponse response;
	
	private String username; //用户名
	private String password; //用户密码
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	//用户登录
	public String userLogin(){
		User user =userService.validateUser(username, password); 
		if(user!=null){	
			session.put("user", user);
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	//展示功能
	public void showFuncs(){
		User user = (User) session.get("user");
		String funcs = userService.getFuncsByUser(user.getUId());
		
		try {
			response.setCharacterEncoding("utf8");
			PrintWriter pw =response.getWriter();
			pw.write(functionService.getFuncsByParentId(-1, funcs).toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}
}
